package com.siwei.mes.sync.erp;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.entity.erp.Rwdextra;
import com.siwei.mes.service.erp.RwdextraService;
import com.siwei.mes.sync.config.SyncProcessorService;
import com.siwei.mes.sync.erp.entity.ItemorderFinish;
import com.siwei.mes.sync.erp.entity.NewTrwd;
import com.siwei.mes.sync.erp.entity.UpdateMix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * ERP-任务单相关
 *
 * @author yangke
 * @since 2023-12-20 22:01:18
 */
@Api(value = "ERP-任务单")
@RestController
@RequestMapping("erp/trwd")
public class SyncErpErpTrwdController {
    private static final Logger log = LoggerFactory.getLogger("erp");

    @Value("${erp-token}")
    private String ERP_TOKEN;
    @Resource
    private RwdextraService rwdextraService;

    @Resource
    private SyncProcessorService syncProcessorService;

    /**
     * 校验token
     *
     * @param request
     */
    public void checkToken(HttpServletRequest request) {
        String token = request.getHeader(BaseParams.Authorization);
        assertTrue(StringUtils.isBlank(token), "授权token为空");
        assertTrue(!token.equals(ERP_TOKEN), "token不正确");
    }


    @AnonymousAccess
    @PostMapping("newTrwd")
    @ApiModelProperty(value = "新增任务单")
    public Json newTrwd(@RequestBody @Validated NewTrwd newTrwd, HttpServletRequest request) {
        this.checkToken(request);
        log.info("ERP通知新增任务单，入参：【{}】", JSON.toJSON(newTrwd));

        /**
         * 新增任务单并上传到协会   新增操作到协会
         * */
        Rwdextra rwdextra = rwdextraService.selectByFrwdh(newTrwd.getFRwdh());
        assertTrue(rwdextra == null, "ERP任务单信息不存在");

        syncProcessorService.submitRequest(newTrwd);
        return Json.succ().data(rwdextra);
    }


    @AnonymousAccess
    @PostMapping("updateMix")
    @ApiModelProperty(value = "修改配合比")
    public Json updateMix(@RequestBody UpdateMix updateMix, HttpServletRequest request) {
        this.checkToken(request);
        assertTrue(updateMix.getFRwdh() == null, "ERP任务单id为空");
        /**
         * 任务单修改了配合比  需要生成一个试验委托  委托类型是更换配合比  2024-08-15 22:36:20  不考虑
         * 对应任务单下所有的小票 关联关系绑定   只生成快检和批检相关的  2024-08-15 22:36:25  不考虑
         * 全部都是抽检
         * 这个任务单之前的委托 需要关闭  修改pk_experiment_check_itemorder 中对应这个任务单的数据都要清除
         * */
        log.info("ERP通知修改任务单配合比，任务单号：【{}】，基准配比编号：【{}】", updateMix.getFRwdh(), updateMix.getFPhbNo());

        Rwdextra rwdextra = rwdextraService.selectByFrwdh(updateMix.getFRwdh());
        assertTrue(rwdextra == null, "ERP任务单信息不存在");
        /**统一用单列线程处理*/
        syncProcessorService.submitRequest(updateMix);
        return Json.succ();
    }

    /**
     * 小票生产完成  开始生产和第一盘开始生产接口作废  统一用小票生产完成接口
     * 有小票完成 需要修改协会那边的任务单状态为 生产中  如果没有协会任务需要新增一个协会任务
     * 如果累计车数是1  就是 第一盘开始生产
     *
     * @param itemorderFinish
     * @param request
     * @return
     */
    @AnonymousAccess
    @PostMapping("itemorderFinish")
    @ApiModelProperty(value = "小票生产完成")
    public Json itemorderFinish(@RequestBody ItemorderFinish itemorderFinish, HttpServletRequest request) {
        log.info("ERP通知小票生产完成，任务单号：【{}】，小票号：【{}】", itemorderFinish.getFRwdh(), itemorderFinish.getITID());

        this.checkToken(request);
        assertTrue(itemorderFinish.getFRwdh() == null, "ERP任务单id为空");
        assertTrue(itemorderFinish.getITID() == null, "ERP小票id为空");

        syncProcessorService.submitRequest(itemorderFinish);
        return Json.succ();
    }
}
